// pages/new-pages/talent/talent.js
// pages/home/home.js
const app = getApp();
let msgIndex = 0;
import Toast from '../../../component/toast/toast';
import utils from '../../../utils/util.js';
import IMController from '../../../controller/im.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        indicatordots: true,
        autoplay: true,
        interval: 5000,
        duration: 400,
        manIcon: '../../assets/icon/icon-man.png',
        womanIcon: '../../assets/icon/icon-woman.png',
        hasMore: true,
        pageSize: 10,
        pageIndex: -1,
        jobList: [],
        filterParams: null,
        popupObj: {
            isShowPopup: false,
            isAnim: false
        },
        filterLabelFlag: true,
        showCompleteResume: false,
        popupTop: '240px'

    },
    myCatchTouch() {
        return;
    },
    clickCompleteFull() {
        this.setData({
            showCompleteResume: false
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (app.userInfo) {
            console.log(app.userInfo)
            this.doLogin(app.userInfo.Token, app.userInfo.Id);
        } else {
            app.userInfoReadyCallback = res => {
                this.doLogin(app.userInfo.Token, app.userInfo.Id);
            }
        }
        let that = this;
        this.getBannersData();
        this.getQueryJobs('', false);
        this.getHomeFilterParams();

    },
    loaction() {
        // app.checkLoactionAuth();
    },
    doLogin: function (token, account) {
        console.log("yun xin，token:" + token + ",account:" + account);
        new IMController({
            token: token,
            account: account
        })
    },
    getBannersData() {
        let that = this;
        wx.request({
            url: app.globalData.serverAddress.getBanners,
            success: function (res) {
                console.log("getBannersData", res);
                if (res.statusCode == 200) {
                    if (res.data.CodeStatus == 20000) {
                        let imgArr = [];
                        res.data.BodyContent.forEach(function (arr) {
                            imgArr.push(arr.ImgUrl);
                        });
                        that.setData({
                            bannerImgs: imgArr
                        });
                    }
                }
            },
            fail: function (res) {
                console.log(JSON.stringify(res));
            },
            complete: function (res) { }
        });
    },
    getQueryJobs(_data, isReach) {
        let that = this;
        let pageI = ++that.data.pageIndex;
        if (!isReach) {
            pageI = 0;
            that.setData({
                hasMore: true
            });
            wx.showLoading({
                title: '加载中...',
                mask: true
            });
        }
        if (!that.data.hasMore) return;
        let data = {};
        if (_data) {
            data = _data;
            data.pageIndex = pageI;
        } else {
            data = this.getDefaultFilterData(pageI);
        }
        that.setData({
            pageIndex: pageI
        });
        console.log(data);
        wx.request({
            url: app.globalData.serverAddress.getQueryJobs,
            data: data,
            method: 'GET',
            success: function (res) {
                console.log("getQueryJobs response", res);
                if (res.statusCode == 200) {
                    if (res.data.CodeStatus == 20000) {
                        let jobList = res.data.BodyContent.List;
                        let count = res.data.BodyContent.TotalCount;
                        let flag = (that.data.pageIndex + 1) * that.data.pageSize < count;
                        jobList.forEach(function (arr, index) {
                            jobList[index].salaryTime = parseInt((arr.SalaryMin * 1000) / 80);
                        });
                        if (isReach) {
                            jobList = that.data.jobList.concat(jobList);
                        }
                        that.setData({
                            jobList: jobList,
                            hasMore: flag
                        })
                    } else {
                        that.setData({
                            jobList: [],
                            hasMore: false
                        });
                        Toast(res.data.Message);
                    }
                }
            },
            fail: function (res) {
                console.log("getQueryJobs fail", res);
            },
            complete: function (res) {
                wx.hideLoading();
                wx.stopPullDownRefresh();
            }
        });
    },
    getDefaultFilterData(pageI) {
        return {
            experienceId: "",
            educationId: "",
            experienceMin: '-1',
            experienceMax: '-1',
            currentUserInfoId: '',
            positionId: '',
            salaryMin: '0',
            salaryMax: '0',
            pageSize: this.data.pageSize,
            pageIndex: pageI
        };
    },
    getHomeFilterParams() {
        let that = this;
        wx.request({
            url: app.globalData.serverAddress.getHomeFilterParams,
            success: function (res) {
                console.log("getHomeFilterParams response", res);
                if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                    let filterParams = res.data.BodyContent;
                    filterParams.ScoreData[0].active = true;
                    filterParams.WorkExpData[0].active = true;
                    filterParams.SalaryData[0].active = true;
                    filterParams.PositionData[0].active = true;
                    filterParams.StatusData[0].active = true;
                    that.setData({
                        filterParams: filterParams
                    })
                }
            },
            fail: function (res) {
                console.log("getHomeFilterParams fail", res);
            },
            complete: function (res) {

            }
        });
    },
    //banner广告栏点击事件
    clickSwipers(e) {
        wx.navigateTo({
            url: '/pages/new-pages/online-work/online-work'
        });
    },
    selectItem(e) {
        if (!this.checkStatus()) {
            return;
        }
        let index = e.currentTarget.dataset.index;
        let parmas = {
            jobId: this.data.jobList[index].Id,
            jobName: this.data.jobList[index].JobName,
            flag: 2
        }
        wx.navigateTo({
            url: '/pages/compony-pages/job-detail/job-detail?parmas=' + JSON.stringify(parmas)
        });
    },
    clickMore() {
        wx.navigateTo({
            url: '/pages/product-list/product-list?flag=2&nickName=更多',
        })
    },
    checkStatus() {
        if (!app.globalData.isLogin) {
            Toast("请先登录");
            return false;
        }
        return true;
    },

    //弹框学历选择
    selectEducation(e) {
        let index = e.currentTarget.dataset.index;
        let education = this.data.filterParams.EducationData;
        for (let i = 0; i < education.length; i++) {
            education[i].active = false;
        }
        education[index].active = true;
        let key = `filterParams.EducationData`;
        this.setData({
            [key]: education
        });
    },
    //弹框经验选择
    selectWorkExp(e) {
        let index = e.currentTarget.dataset.index;
        let workExp = this.data.filterParams.WorkExpData;
        for (let i = 0; i < workExp.length; i++) {
            workExp[i].active = false;
        }
        workExp[index].active = true;
        let key = `filterParams.WorkExpData`;
        this.setData({
            [key]: workExp
        });
    },
    //弹框薪资选择
    selectSalary(e) {
        let index = e.currentTarget.dataset.index;
        let salary = this.data.filterParams.SalaryData;
        for (let i = 0; i < salary.length; i++) {
            salary[i].active = false;
        }
        salary[index].active = true;
        let key = `filterParams.SalaryData`;
        this.setData({
            [key]: salary
        });
    },
    //点击弹框空白处
    clickFull() {
        let data = {
            popupObj: this.data.popupObj
        };
        data.popupObj.isShowPopup = false;
        data.popupObj.isAnim = false;
        this.setData(data);
    },
    //要求条件事件
    clickFilter(e) {
        let that = this;
        //根据要求标签的相对位置改变弹出框的弹出位置
        const query = wx.createSelectorQuery()
        query.select('#filter').boundingClientRect(function (res) {
            that.setData({
                popupTop: (res.top + res.height) + "px"
            })
        })
        query.exec()
        let data = {
            popupObj: this.data.popupObj
        };
        data.popupObj.isShowPopup = !data.popupObj.isShowPopup;
        data.popupObj.isAnim = !data.popupObj.isAnim;
        this.setData(data);
    },
    //职能筛选
    clickFilterLabel() {
        let parmas = this.data.filterParams.PositionData;
        wx.navigateTo({
            url: '/pages/new-pages/filter-label/filter-label?parmas=' + JSON.stringify(parmas)
        })
    },
    //弹框重置条件
    clickReset() {
        let filterParams = this.data.filterParams;
        let workExp = filterParams.WorkExpData;
        for (let i = 1; i < workExp.length; i++) {
            workExp[i].active = false;
        }
        workExp[0].active = true;
        let salary = filterParams.SalaryData;
        for (let i = 1; i < salary.length; i++) {
            salary[i].active = false;
        }
        salary[0].active = true;
        let education = filterParams.EducationData;
        for (let i = 1; i < education.length; i++) {
            education[i].active = false;
        }
        education[0].active = true;
        this.setData({
            filterParams: filterParams
        })
    },
    //弹框确定条件
    clickConfirm() {
        let data = this.getSelectFilterData();
        this.getQueryJobs(data, false);
        let popupObj = this.data.popupObj;
        popupObj.isAnim = false;
        popupObj.isShowPopup = false;
        this.setData({
            popupObj: popupObj
        })
    },
    //通过index将点击的职能active设置为true
    setFromIndexActive(index) {
        let wayList = this.data.filterParams.PositionData;
        for (let i = 0; i < wayList.length; i++) {
            wayList[i].active = false;
        }
        wayList[index].active = true;
        let key = `filterParams.PositionData`
        this.setData({
            [key]: wayList
        });
    },
    //获取当前以选中的要求数据
    getSelectFilterData() {
        let educations = this.data.filterParams.EducationData;
        let eduObj = {};
        for (let i = 0; i < educations.length; i++) {
            if (educations[i].active) {
                eduObj = educations[i];
                break;
            }
        }
        let salarys = this.data.filterParams.SalaryData;
        let salaryObj = {};
        for (let i = 0; i < salarys.length; i++) {
            if (salarys[i].active) {
                salaryObj = salarys[i];
                break;
            }
        }
        let workExp = this.data.filterParams.WorkExpData;
        let worExpObj = {};
        for (let i = 0; i < workExp.length; i++) {
            if (workExp[i].active) {
                worExpObj = workExp[i];
                break;
            }
        }
        //获取已选择的职能id
        let wayList = this.data.filterParams.PositionData;
        let wayObj = {};
        for (let i = 0; i < wayList.length; i++) {
            if (wayList[i].active) {
                wayObj = wayList[i];
                break;
            }
        }
        let that = this;
        let data = {
            experienceId: "",
            educationId: "",
            currentUserInfoId: '',
            positionId: '',
            salaryMin: '0',
            salaryMax: '0',
            pageSize: 10,
            pageIndex: 0
        };
        data.positionId = wayObj.Id;
        data.experienceId = worExpObj.id;
        data.educationId = eduObj.Id;
        data.salaryMin = salaryObj.min;
        data.salaryMax = salaryObj.max;
        return data;
    },
    //弹框拦截滑动事件
    interceptCatchMove() { },
    //拿红包
    clickEnvelopes() {
        this.setData({
            showCompleteResume: true
        })
    },
    //完善简历
    toCompleteResume() {
        if (!this.checkStatus()) {
            setTimeout(function () {
                wx.switchTab({
                    url: '/pages/me/me'
                })
            }, 800);
        } else {
            wx.navigateTo({
                url: '/pages/new-pages/main-page/main-page'
            });
        }
        this.setData({
            showCompleteResume: false
        })
    },
    /**
     * 点击搜索框
     */
    clickSearch() {
        wx.navigateTo({
            url: '/pages/new-pages/talent-search/talent-search?flag=0'
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

        // if (this.data.msgArr.length > 0){
        //     this.startMsgLooper();
        // }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        this.clickFull();
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () { },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        wx.showLoading({
            title: '加载中...',
            mask: true
        })
        let data = this.getSelectFilterData();
        this.getQueryJobs(data, false);
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let data = this.getSelectFilterData();
        this.getQueryJobs(data, true);
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})